package org.example.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.erp.domain.*;
import org.example.erp.service.*;
import org.example.erp.mapper.ProClientOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【pro_client_order(客户订单)】的数据库操作Service实现
 * @createDate 2024-11-13 18:13:22
 */
@Service
public class ProClientOrderServiceImpl extends ServiceImpl<ProClientOrderMapper, ProClientOrder>
        implements ProClientOrderService {

    @Autowired
    ProductBomService productBomService;
    @Autowired
    UnitsService unitsService;
    @Autowired
    InventoryService inventoryService;
    @Autowired
    ProClientOrderItemServiceImpl proClientOrderItemService;
    @Autowired
    ProWorkorderBomService proWorkorderBomService;

    public Page<ProClientOrder> findTableAssociation(LambdaQueryWrapper<ProClientOrder> queryWrapper, Page<ProClientOrder> page) {
//        return page.setRecords(baseMapper.findTableAssociation(queryWrapper, page));
        return this.page(page,queryWrapper);
    }

    @Transactional
    public boolean add(ProClientOrder proClientOrder) {
        proClientOrder.setCreateTime(new Date());
        boolean save = save(proClientOrder);
        // 查询产品是否有原材料
        List<ProductBom> byId = productBomService.findById(proClientOrder.getProductId());


        if (byId != null) {
            int number = 1;
            for (ProductBom p : byId) {
                List<Inventory> inventoryList = inventoryService.findInventoryById(p.getBomMaterialCode());//查询这个产品的Bom材料是否有库存

                QueryWrapper<Units> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Units::getUnitName, p.getBomUnit());
                Units list = unitsService.getOne(queryWrapper);

                ProClientOrderItem proClientOrderItem = new ProClientOrderItem();
                proClientOrderItem.setItemId(p.getBomMaterialId());
                proClientOrderItem.setItemCode(p.getBomMaterialCode());
                proClientOrderItem.setItemName(p.getBomMaterialName());
                proClientOrderItem.setSpecification(p.getBomMaterialSpec());
                proClientOrderItem.setUnitName(p.getBomUnit());
                proClientOrderItem.setUnitOfMeasure(list.getUnit());
                proClientOrderItem.setClientOrderId(proClientOrder.getClientOrderId());
                proClientOrderItem.setClientOrderCode(proClientOrder.getClientOrderCode());
                proClientOrderItem.setLevel(number + "");
                number++;

                if (inventoryList.size()!=0){//如果查询出存在库存
                    boolean s = proClientOrderItemService.save(proClientOrderItem);//添加客户订单Bom材料
                }

            }
        }
        return save;
    }

    @Transactional
    public boolean updates(ProClientOrder proClientOrder){//根据传参给主表更新工单信息
        boolean s=true;
        boolean b = updateById(proClientOrder);
        if (!b){
            s=false;
        }
        QueryWrapper<ProClientOrderItem> q1=new QueryWrapper<>();
        q1.lambda().eq(ProClientOrderItem::getClientOrderId,proClientOrder.getClientOrderId());
        List<ProClientOrderItem> list = proClientOrderItemService.list(q1);
        for (ProClientOrderItem p:list){
            p.setWorkorderId(proClientOrder.getWorkorderId());
            p.setWorkorderCode(proClientOrder.getWorkorderCode());
            p.setWorkorderName(proClientOrder.getWorkorderName());
            boolean b1 = proClientOrderItemService.updateById(p);//更新订单从表的工单信息
            if (!b1){
                s=false;
                break;
            }
        }
        return s;
    }

}




